import torch
from torch import nn


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.VDEncoder = nn.Sequential(
            nn.Linear(in_features=1, out_features=128),
            nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv1d(in_channels=64, out_channels=1, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Linear(in_features=128, out_features=512),
            nn.TransformerEncoderLayer(d_model=512, nhead=8, dim_feedforward=2048)
        )
        self.RSDEncoder = nn.Sequential(
            nn.Linear(in_features=1, out_features=128),
            nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv1d(in_channels=64, out_channels=1, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Linear(in_features=128, out_features=512),
            nn.TransformerEncoderLayer(d_model=512, nhead=8, dim_feedforward=2048)
        )
        self.GSCEncoder = nn.Linear(in_features=1, out_features=128)
        self.Decoder = nn.Sequential(
            nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3),
            nn.AvgPool1d(32),
            nn.Conv1d(in_channels=32, out_channels=1, kernel_size=3),
            nn.AvgPool1d(32),
            nn.Tanh()
        )

    def forward(self, Vibration, Rotation, Gear):
        F_v = self.VDEncoder(Vibration.unsqueeze(1))
        F_r = self.RSDEncoder(Rotation.unsqueeze(1))
        F_g = self.GSCEncoder(Gear.unsqueeze(1))
        output = self.Decoder(torch.cat([F_v, F_r, F_g], dim=-1))
        return output + 1
